﻿using CallCenter.Data.Base;
using CallCenter.Data.BizUtilities.Base;
using CallCenter.Data.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace CallCenter.Data.BizUtilities
{
   public class ClientTypeUtility: BaseUtility
    {
        public ClientTypeUtility(DataManager manager)
            :base(manager)
        { }
        public ReturnMessage<ClientTypeViewModel> InsertToClient(ClientTypeViewModel model)
        {
            var returnMessage = new ReturnMessage<ClientTypeViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            if (String.IsNullOrEmpty(model.description))
                model.description = "";
            var id = this.Insert(model.clientTypeCode,model.clientTypeName,model.description);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }

        public ReturnMessage<ClientTypeViewModel> UpdateToClientType(ClientTypeViewModel model)
        {
            var returnMessage = new ReturnMessage<ClientTypeViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {

            if (String.IsNullOrEmpty(model.description))
                model.description = "";
            var id = this.update(model.id,model.clientTypeCode,model.clientTypeName,model.description);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }


        public List<ClientTypeViewModel> GetAll() //Gets all the data form the contacts table
        {
            string sql = "Select * from ddlClientType";

            return this.Factory.ExecuteObjects<ClientTypeViewModel>(sql);
        }
        public int Insert(string clientTypeCode, string clientTypeName, string description) //insert data to a contact table
        {
            int nRtn = -1;
            string sql = "INSERT INTO ddlClientType(clientTypeCode, clientTypeName, description) Values(@clientTypeCode, @clientTypeName, @desc) SELECT @pResult =@@IDENTITY";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@clientTypeCode", SqlDbType.NVarChar, 10, clientTypeCode);
                this.Factory.AddParameter(cmd, "@clientTypeName", SqlDbType.NVarChar, 100, clientTypeName);
                this.Factory.AddParameter(cmd, "@desc", SqlDbType.NVarChar, 100, description);

                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;

        }
        public int update(int id, string clientTypeCode, string clientTypeName, string description) //insert data to a contact table
        {
            int nRtn = -1;
            string sql = "UPDATE ddlClientType SET clientTypeCode=@clientTypeCode, clientTypeName=@clientTypeName, description=@desc WHERE id =@id";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@clientTypeCode", SqlDbType.NVarChar, 10, clientTypeCode);
                this.Factory.AddParameter(cmd, "@clientTypeName", SqlDbType.NVarChar, 1000, clientTypeName);
                this.Factory.AddParameter(cmd, "@desc", SqlDbType.NVarChar, 100, description);
                this.Factory.AddParameter(cmd, "@id", SqlDbType.Int, 10, id);
                this.Factory.ExecuteNonQuery(cmd);

            }
            nRtn = id;
            return nRtn; //retruning the data stored in the table
        }


        public void Delete(int nAddressType) //delete the row of data using the id passed to the function
        {
            string sql = "DELETE FROM ddlClientType " +
                  "WHERE ID = @ID ";


            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 10, nAddressType);
                this.Factory.ExecuteNonQuery(cmd);
            }

        }

        public void DeleteAll() //delete all the rows in the data.
        {
            string sql = "DELETE FROM ddlClientType ";
            this.Factory.ExecuteTable(sql);

        }


    }
}